package com.mida.bms.report.common.massert;


import com.mida.tool.common.exception.MassertException;
import com.mida.tool.common.massert.MAssert;
import com.mida.tool.common.result.RetMsg;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;

/**
 * Created by yutao on 2017/4/20.
 */
public class MRAssert extends MAssert {

    public static void massertIsEmpty(Object obj, RetMsg retMsg) throws MassertException {

        if (!objectIsEmpty(obj)) {
            throw new MassertException(retMsg);
        }
    }
    public static void massertNotEmpty(Object obj, RetMsg retMsg) throws MassertException{

        if (objectIsEmpty(obj)) {
            throw new MassertException(retMsg);
        }
    }
    public static void massertIn(RetMsg retMsg,Object inObj, Object ... tarObjs) throws MassertException{

        for(Object obj:tarObjs){
            if(inObj==null){
                if(obj==null){
                    return;
                }
            }
            if(inObj.equals(obj)){
                return;
            }
        }
        throw new MassertException(retMsg);
    }

    public static void massertNotIn(RetMsg retMsg,Object inObj, Object ... tarObjs) throws MassertException{

        for(Object obj:tarObjs){
            if(inObj==null){
                if(obj==null){
                    throw new MassertException(retMsg);
                }
            }
            if(inObj.equals(obj)){
                throw new MassertException(retMsg);
            }
        }

    }

    private static boolean objectIsEmpty(Object obj) {
        if (obj == null) {
            return true;
        }

        if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection<?>) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map<?, ?>) obj).isEmpty();
        }
        // else
        return false;
    }
}